<template>
  <el-dialog :title="title" append-to-body :destroy-on-close="true" :visible.sync="visible" :width="'560px'" :before-close="closeDialog">
    <!-- :disabled="isLookMode" -->
    <!-- <el-form :model="rateForm" ref="rateForm" :rules="rules" label-width="100px" size="small" > -->
    <el-row>
      <el-col :span="24">
        <div class="el-form-item">
          <div class="el-form-item el-form-item--medium fl">
            <label class="el-form-item__label">分类名称：</label>
          </div>

          <div class="fl pad_top">
            <span v-if="data.title">{{ data.title }}</span>
            <span v-else>无</span>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="el-form-item">
          <div class="el-form-item el-form-item--medium fl">
            <label class="el-form-item__label">父类：</label>
          </div>
          <div class="fl pad_top">
            <span v-if="data.relationRule">{{ data.relationRule }}</span>
            <span v-else>无</span>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="el-form-item">
          <div class="el-form-item el-form-item--medium fl">
            <label class="el-form-item__label">分类编码：</label>
          </div>
          <div class="fl pad_top">
            <span v-if="data.categoryNo">{{ data.categoryNo }}</span>
            <span v-else>无</span>
          </div>
          <!-- </div> -->
        </div>
      </el-col>
    </el-row>
    <el-row v-if="type == 2">
      <el-col :span="24">
        <div class="el-form-item">
          <el-button @click="updateRateCode">更改税码</el-button>
        </div>
      </el-col>
    </el-row>
    <rate-list :visible.sync="isShowRateDialog" :selectedList="pageDataed" @selected="getSelectProperty"></rate-list>
    <div class="full-width" id="mul_select" v-if="type == 1">
      <el-form :inline="true" size="small" :ref="search">
        <el-form-item label="分类名称/简称/说明/关键字：">
          <el-input size="small" placeholder="请输入分类名称/简称/说明/关键字" v-model.trim="search.name" maxlength="50" clearable ></el-input>
        </el-form-item>
        <el-form-item label="分类编码/分类简码：">
          <el-input size="small" placeholder="请输入分类编码/分类简码" v-model.trim="search.number" maxlength="50" clearable ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" class="width_104" @click="getPageDataList()">查询</el-button>
          <el-button size="small" class="width_104" @click="resetOption()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="full-width" id="mul_select" v-else>
      <el-form :inline="true" size="small" :ref="search">
        <el-form-item label="分类名称/简称/说明/关键字：">
          <el-input size="small" placeholder="请输入分类名称/简称/说明/关键字" v-model.trim="search.name" maxlength="50" clearable ></el-input>
        </el-form-item>
        <el-form-item label="分类编码/分类简码：">
          <el-input size="small" placeholder="请输入分类编码/分类简码" v-model.trim="search.number" maxlength="50" clearable ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" class="width_104" @click="getBindedList()">查询</el-button>
          <el-button size="small" class="width_104" @click="resetBindedOption()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-if="type == 1">
      <el-table class="margin_top_20" ref="multipleTableRef" :data="pageData" :row-key="getRowKeys" stripe @selection-change="handleSelectionChange" :header-cell-style="tableHeaderStyle" v-loading="loading">
        <el-table-column type="selection" width="42" :reserve-selection="true"></el-table-column>
        <el-table-column prop="title" label="分类名称"></el-table-column>
        <el-table-column prop="taxNumber" label="分类编码"></el-table-column>
        <el-table-column prop="shortNumber" label="分类简码"></el-table-column>
        <el-table-column prop="taxRate" label="税率">
          <template slot-scope="scope">
            <span>{{ Number(scope.row.taxRate) * 100 }}%</span>
          </template>
        </el-table-column>
        <el-table-column prop="abbreviation" label="简称"></el-table-column>
        <el-table-column prop="stated" label="说明"></el-table-column>
        <el-table-column prop="keywords" label="关键字"></el-table-column>
        <!-- <el-table-column prop="createTime" label="时间"></el-table-column> -->
      </el-table>
      <!-- 页面页脚 -->
      <div class="text-center">
        <br/>
        <el-pagination background @size-change="handleSizeChange" @current-change="handlePagingChange" :current-page="pageIndex" :page-sizes="pageSizes" :page-size="pageSize" :layout="paginationLayout" :total="pageCount"> </el-pagination>
      </div>
    </div>
    <div v-else>
      <el-table class="margin_top_20" ref="multipleTableRef" :data="pageDataed" stripe :header-cell-style="tableHeaderStyle" v-loading="loading">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="title" label="分类名称"></el-table-column>
        <el-table-column prop="taxNumber" label="分类编码"></el-table-column>
        <el-table-column prop="shortNumber" label="分类简码"></el-table-column>
        <el-table-column prop="taxRate" label="税率">
          <template slot-scope="scope">
            <span>{{ Number(scope.row.taxRate) * 100 }}%</span>
          </template>
        </el-table-column>
        <el-table-column prop="abbreviation" label="简称"></el-table-column>
        <el-table-column prop="stated" label="说明"></el-table-column>
        <el-table-column prop="keywords" label="关键字"></el-table-column>
      </el-table>
      <!-- 页面页脚 -->
      <div class="text-center">
        <br/>
        <el-pagination background @size-change="handleSizeChange" @current-change="handlePagingChange" :current-page="pageIndex" :page-sizes="pageSizes" :page-size="pageSize" :layout="paginationLayout" :total="pageCount"> </el-pagination>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelModel()">取 消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="closeModel">确 定</el-button>
    </div>
  </el-dialog>
  
</template>

<script>
import Setting from '@/settings'
import RateList from '@/views/store-management/components/rateList';
import { BindRateCode, EditFincialData, GetFinicalDataList, GetBindedRateList } from '@/views/financial-management/services/index'
export default {
    components: {
        RateList
    },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    data: {
      type: Object,
      default: {},
    },
     updateType: {
       type: Number,
       default: 1
     }
  },
  data() {
    return {
      pageIndex: 1,
      pageSize: Setting.defaultTablePaginationSize,
      pageSizes: Setting.tablePaginationSizes,
      tableHeaderStyle: Setting.tableHeaderStyle,
      paginationLayout: Setting.tablePaginationLayout,
      pageData: [],
      pageDataed: [],
      pageCount: 0,
      loading: false,
      multipleSelection: [],
      submitLoading: false,
      search: {
        title: '',
        name: '',
        number: '',
      },
      type: 1,
      isShowRateDialog: false,
      status: [
        {
          value: 0.13,
          name: '13%',
        },
        {
          value: 0.06,
          name: '6%',
        },
        {
          value: 0.09,
          name: '9%',
        },
      ],
    }
  },
  watch: {
    visible: {
      immediate: true,
      handler(newValue) {
        if (!newValue) return
        // this.getPageDataList()
      },
    },
    updateType: {
      immediate: true,
      handler(newValue) {
        if (!newValue) return
        this.type = newValue
        if(this.type == 1) {
            this.getPageDataList()
        }else {
            this.getBindedList()
        }
      },
    },
  },
  methods: {
    closeDialog() {
        this.clearForm()
        this.$emit('update:visible', false)
      },
    watchInput(event, key) {},
    updateRateCode() {
       this.isShowRateDialog = true
    },
    resetBindedOption(){
     this.search.name = '';
     this.search.number = '';
     this.pageIndex = 1;
     this.getBindedList();
    },
    resetOption(){
     this.search.name = '';
     this.search.number = '';
     this.pageIndex = 1;
     this.getPageDataList();
    },
    getSelectProperty(list) {
      this.pageDataed = list
      this.pageCount = this.pageDataed.length
      this.pageIndex = 1
      this.multipleSelection = list
    },
    closeModel() {
      console.log(this.data)
      if (this.multipleSelection.length == 0) {
        this.$message.warning('请选择税率税码！')
        return
      }
      let Ids = this.multipleSelection.map(item => {
        return item.id
      })
      BindRateCode({
        id: this.data.id,
        targetIds: Ids.join(','),
      }).then(res => {
        if (res.code == 200) {
          this.$message.success('绑定成功！')
        } else {
          this.$message.error(res.msg)
        }
        this.clearForm()
        this.$emit('update:visible', false)
        this.$emit('update')
      })
    },
    cancelModel() {
      this.clearForm()
      this.$emit('update:visible', false)
    },
    getRowKeys(row) {
      return row.id // 每条数据的唯一识别值
    },
    handleSelectionChange(selectlist) {
      this.multipleSelection = selectlist
      console.log(this.multipleSelection)
    },
    getBindedList(){
       GetBindedRateList({
           id: this.data.id,
           name: this.search.name,
           number: this.search.number,
           pageNum: this.pageIndex,
           pageSize: this.pageSize
       }).then(res =>{
           if(res.code == 200) {
               this.pageDataed = res.data.list
               this.multipleSelection = res.data.list
               this.pageCount = res.data.total
               if ((!res.data.list || !res.data.list.length) && this.pageIndex > 1) {
              this.pageIndex = 1
              this.getBindedList()
            }
           }
       }).catch(err =>{
           console.log(err)
       })
    },
    getPageDataList() {
      this.loading = true
      GetFinicalDataList({
        name: this.search.name,
        number: this.search.number,
        pageNum: this.pageIndex,
        pageSize: this.pageSize
      })
        .then(res => {
          if (res.code == 200) {
            this.pageData = res.data.list
            this.pageCount = res.data.total
            if ((!res.data.list || !res.data.list.length) && this.pageIndex > 1) {
              this.pageIndex = 1
              this.getPageDataList()
            }
          } else {
          }
          this.loading = false
        })
        .catch(err => {
          console.log(err)
        })
      //   this.rateForm.title = this.data.title;
      //   this.rateForm.taxNumber = this.data.taxNumber;
      //   this.rateForm.abbreviation = this.data.abbreviation;
      //   this.rateForm.stated = this.data.stated;
      //   this.rateForm.keywords = this.data.keywords;
      //   this.rateForm.taxRate = this.data.taxRate;
      //   this.rateForm.status = this.data.status
    },
    // 分页操作
    handlePagingChange(event) {
      this.pageIndex = event
      if(this.type == 1) {
      this.getPageDataList()
      }else  {
        this.getBindedList()
      }
    },
    handleSizeChange(event) {
      this.pageIndex = 1
      this.pageSize = event
      if(this.type == 1) {
      this.getPageDataList()
      }else  {
        this.getBindedList()
      }
    },
    //清空表单信息
    clearForm() {
      // this.search.title = ''
      this.search.name = ''
      this.search.number = ''
    //   ;(this.search.abbreviation = ''), (this.search.explain = ''), (this.search.keywords = ''), (this.search.status = 0)
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog {
  min-width: 80%;
  min-height: 80%;
}
.pad_top {
  padding-top: 10px;
}
</style>
